import store from './store';
import routes from "../router/index";

function filterChildren(list, role) {
    const result = [];

    if (list && list.length > 0) {
        list.forEach(item => {
            // 如果菜单隐藏就跳过
            if (item.hide) return;

            // 判断当前用户是否拥有足够权限
            if (!item.roles || item.roles.indexOf(role) >= 0) {
                const temp = {};

                temp.index = item.path || '';
                temp.title = item.title || '未知';
                temp.icon = item.iconFont || 'el-icon-message';

                if (item.subs && item.subs.length > 0) {
                    temp.subs = filterChildren(item.subs, role);
                }
                result.push(temp);
            }
        });
    }
    console.log(result);

    return result;
}

export default {
    get menus() {
        const _routes = routes;
        const _role = store.getters.userinfo.role;
        // const _role = 'admin';
        console.log(_role);
        for (const item of _routes) {
            // console.log(item);
            if (item.some === "home") {

                return filterChildren(item.children, _role);
            }
        }

        return [];
    }
}
